<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
    <el-form-item label="禁用状态" prop="companyStatus">
            <el-select
              v-model="queryParams.companyStatus"
              placeholder="用户状态"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="dict in dict.type.company_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
      <el-form-item label="名字" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入名字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="companyPerson">
        <el-input
          v-model="queryParams.companyPerson"
          placeholder="请输入联系人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="companyPhone">
        <el-input
          v-model="queryParams.companyPhone"
          placeholder="请输入电话"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          type="number"
          oninput="if(value.length>11)value=value.slice(0,11)"
        />
      </el-form-item>
      <el-form-item label="地址" prop="companyAddress">
        <el-input
          v-model="queryParams.companyAddress"
          placeholder="请输入地址"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名字" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账户" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账户"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账户名字" prop="bankAccountName">
        <el-input
          v-model="queryParams.bankAccountName"
          placeholder="请输入银行账户名字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
            <el-date-picker
            @change="timecahnge"
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:unit:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['areas:remove']"
          >批量删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="120" />

      <el-table-column label="禁用状态" key="companyStatus" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.companyStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="名字"
        align="center"
        prop="companyName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="companyPerson"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="电话"
        align="center"
        prop="companyPhone"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="地址"
        align="center"
        prop="companyAddress"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="法人身份证正面"
        align="center"
        prop="legalImgFront"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template slot-scope="scope">
          <el-image
          v-if="scope.row.legalImgFront"
            class="img"
            :src="scope.row.legalImgFront"
            :preview-src-list="[scope.row.legalImgFront]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="法人身份证反面"
        align="center"
        prop="legalImgBack"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template slot-scope="scope">
          <el-image
          v-if="scope.row.legalImgBack"
            class="img"
            :src="scope.row.legalImgBack"
            :preview-src-list="[scope.row.legalImgBack]"
          ></el-image>
        </template>
      </el-table-column>
 
      <el-table-column
        label="银行名字"
        align="center"
        prop="bankName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="银行账户"
        align="center"
        prop="bankAccount"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="银行账户名字"
        align="center"
        prop="bankAccountName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="统一社会信用代码"
        align="center"
        prop="companyUscc"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="营业执照"
        align="center"
        prop="licenseImg"
        :show-overflow-tooltip="true"
        width="150"
      >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.licenseImg"
            class="img"
            :src="scope.row.licenseImg"
            :preview-src-list="[scope.row.licenseImg]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['areas:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['areas:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="名字" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="联系人" prop="companyPerson">
          <el-input v-model="form.companyPerson" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="电话" prop="companyPhone">
          <el-input v-model="form.companyPhone" placeholder="请输入电话" 
          onkeyup="this.value = this.value.replace(/[^\d]/g,'');"
          type="number"
          oninput="if(value.length>11)value=value.slice(0,11)"
          />
          
        </el-form-item>
        <el-form-item label="地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入单位" />
          
        </el-form-item>
        <div style="display: flex;justify-content: space-between;">
          <el-form-item label="法人身份证正面" prop="legalImgFront">
          <upimg
            :up-with="240"
            :imgSrc="form.legalImgFront"
            :up-height="160"
            :up-bradius="10"
            uptext="上传法人身份证正面"
            @changeName="imgUrlfun1"
          ></upimg>
        </el-form-item>
        <el-form-item label="法人身份证反面" prop="legalImgBack">
          <upimg
            :up-with="240"
            :imgSrc="form.legalImgBack"
            :up-height="160"
            :up-bradius="10"
            uptext="上传法人身份证反面"
            @changeName="imgUrlfun2"
          ></upimg>
        </el-form-item>
        </div>
        <el-form-item label="银行名字" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入单位" />
          
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入单位" />
          
        </el-form-item>
        <el-form-item label="银行账户名字" prop="bankAccountName">
          <el-input v-model="form.bankAccountName" placeholder="请输入单位" />
          
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="companyUscc">
          <el-input v-model="form.companyUscc" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="营业执照" prop="licenseImg">
          <upimg
            :up-with="240"
            :imgSrc="form.licenseImg"
            :up-height="160"
            :up-bradius="10"
            uptext="上传营业执照"
            @changeName="imgUrlfun3"
          ></upimg>
        </el-form-item>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCompanyList,
  CompanyInfo,
  updateCompany,
  delCompany,
  addCompany ,
} from "@/api/zhao/company.js";
import upimg from "../../components/UploadImg/index.vue";
export default {
  dicts: ['company_status'],
  components: {
    upimg,
  },
  name: "Role",
  data() {
    var checkPhone = (rule, value, callback) => { // 手机号验证
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else {
        const reg = /^1[3456789]\d{9}$/
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 日期范围
      dateRange: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyStatus: undefined,
        companyName: undefined,
        companyPerson: undefined,
        companyPhone: undefined,
        companyAddress: undefined,
        bankName: undefined,
        bankAccount: undefined,
        bankAccountName: undefined,
        companyUscc: undefined,
        beginTime: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "名字不能为空", trigger: "blur" },
        ],
        companyPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        companyPhone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          {validator: checkPhone, trigger: 'blur'}
        ],
        companyAddress: [
          { required: true, message: "地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 角色状态修改
    handleStatusChange(row) {
      console.log( row.companyStatus);
      let text = row.companyStatus == 1 ? "禁用" : "开启";
      this.$modal
        .confirm('确认要"' + text + '""' + row.companyName + '"角色吗？')
        .then(function () {
          return updateCompany({
            id: row.id,
            companyStatus:  row.companyStatus,
          });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
           row.companyStatus = row.companyStatus === 0 ? 1 : 0;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        companyStatus: 0,
        companyName: '',
        companyPerson: '',
        companyPhone: '',
        companyAddress: '',
        bankName: '',
        bankAccount: '',
        bankAccountName: '',
        companyUscc: '',
        beginTime: '',
        endTime: '',
      };
      this.resetForm("form");
    },
    // 接受组件传递过来的值 也就是上传完成接口的返回
    imgUrlfun1(e) {
      if (e) {
        this.form.legalImgFront = e.url;
      } else {
        this.form.legalImgFront = "";
      }
    },
    imgUrlfun2(e) {
      if (e) {
        this.form.legalImgBack = e.url;
      } else {
        this.form.legalImgBack = "";
      }
    },
    imgUrlfun3(e) {
      if (e) {
        this.form.licenseImg = e.url;
      } else {
        this.form.licenseImg = "";
      }
    },
    timecahnge(e){
        this.queryParams.beginTime = e[0]
        this.queryParams.endTime = e[1]
    },
    getList() {
      this.loading = true;
      getCompanyList(this.queryParams).then((res) => {
        this.roleList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
       this.dateRange = [];
      console.log( this.queryParams.beginTime = '');
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleUpdate(row) {
      const postId = row.id || this.ids;
      CompanyInfo({ id: postId }).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      console.log(this.ids);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let postIds = "";
      if (row.id) {
        postIds = [row.id];
      } else {
        postIds = this.ids;
      }
      this.$modal
        .confirm('是否确认删除ID编号为"' + postIds + '"的数据项？')
        .then(function () {
          delCompany({ ids: postIds }).then((res) => {
            console.log(res);
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      console.log(this.form);
      this.open = true;
      this.title = "添加外包数据";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCompany(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style>
.img {
  width: 120px;
  height: 60px;
}
</style>
